﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KATS_Client.Client
{
    /// <summary>
    /// This handles passing on all the server functions to the server
    /// </summary>
    public static class ServerFunctions
    {
        /// <summary>
        /// The connection to the server
        /// </summary>
        private static KATS_ServerReference.ServerClient server
        {
            get { return ServerConnection.Server; }
        }

        /// <summary>
        /// Close the open file
        /// </summary>
        public static void CloseFile()
        {
            server.CloseFile();
        }

        /// <summary>
        /// Close the file reader
        /// </summary>
        public static void CloseReader()
        {
            server.CloseReader();
        }

        /// <summary>
        /// Close the file writer
        /// </summary>
        public static void CloseWriter()
        {
            server.CloseWriter();
        }

        /// <summary>
        /// Copy the file
        /// </summary>
        /// <param name="target">Target to copy to</param>
        /// <returns>Successful?</returns>
        public static bool Copy(string target)
        {
            return server.Copy(Encryption.Encrypt(target));
        }

        /// <summary>
        /// Create a directory
        /// </summary>
        /// <param name="location">Where?</param>
        /// <returns>Successful?</returns>
        public static bool CreateDirectory(string location)
        {
            KATS_ServerReference.ServerClient server = ServerConnection.MakeConnection();
            bool cd = server.CreateDirectory(Encryption.Encrypt(location));
            server.Disconnect();
            return cd;
        }

        /// <summary>
        /// Delete
        /// </summary>
        /// <returns>Successful?</returns>
        public static bool Delete()
        {
            return server.Delete();
        }

        /// <summary>
        /// Does object exist?
        /// </summary>
        /// <returns>Successful?</returns>
        public static bool Exists()
        {
            bool e = server.Exists();
            return e;
        }

        /// <summary>
        /// Does object exist?
        /// </summary>
        /// <param name="loc">The object</param>
        /// <returns>Exists?</returns>
        public static bool Exist(string loc)
        {
            KATS_ServerReference.ServerClient server = ServerConnection.MakeConnection();
            bool e = server.Exist(Encryption.Encrypt(loc));
            server.Disconnect();
            return e;
        }

        /// <summary>
        /// File size
        /// </summary>
        /// <param name="location">The file</param>
        /// <returns>File size, in bytes</returns>
        public static long FileLength(string location)
        {
            KATS_ServerReference.ServerClient server = ServerConnection.MakeConnection();
            long size = server.FileLength(Encryption.Encrypt(location));
            server.Disconnect();
            return size;
        }

        /// <summary>
        /// Directory size
        /// </summary>
        /// <param name="location">The directory</param>
        /// <param name="recursive">Include subfolders?</param>
        /// <returns>Directory size [bytes]</returns>
        public static long DirectoryLength(string location, bool recursive)
        {
            KATS_ServerReference.ServerClient server = ServerConnection.MakeConnection();
            long size = server.DirectoryLength(Encryption.Encrypt(location), recursive);
            server.Disconnect();
            return size;
        }

        /// <summary>
        /// The files in the directory
        /// </summary>
        /// <returns>The files</returns>
        public static string[] GetDirectoryFiles()
        {
            KATS_ServerReference.ServerClient server = ServerConnection.MakeConnection();
            string[] encryptedFiles = server.GetDirectoryFiles();
            server.Disconnect();
            string[] decryptedFiles = new string[encryptedFiles.Length];
            for (int i = 0; i < encryptedFiles.Length; i++)
            {
                decryptedFiles[i] = Encryption.Decrypt(encryptedFiles[i]);
            }
            return decryptedFiles;
        }

        /// <summary>
        /// The files in the directory
        /// </summary>
        /// <param name="location">The directory</param>
        /// <returns>The files</returns>
        public static string[] GetDirectoryFilesS(string location)
        {
            KATS_ServerReference.ServerClient server = ServerConnection.MakeConnection();
            string[] encryptedFiles = server.GetDirectoryFilesS(Encryption.Encrypt(location));
            server.Disconnect();
            string[] decryptedFiles = new string[encryptedFiles.Length];
            for (int i = 0; i < encryptedFiles.Length; i++)
            {
                decryptedFiles[i] = Encryption.Decrypt(encryptedFiles[i]);
            }
            return decryptedFiles;
        }

        /// <summary>
        /// The files in the directory and their sizes. Separated with a ?
        /// </summary>
        /// <param name="location">The directory</param>
        /// <returns>The files and sizes. Separated with a ?</returns>
        public static string[] GetDirectoryFilesAndSizes(string location)
        {
            KATS_ServerReference.ServerClient server = ServerConnection.MakeConnection();
            string[] encryptedFiles = server.GetDirectoryFilesAndSizes(Encryption.Encrypt(location));
            server.Disconnect();
            string[] decryptedFiles = new string[encryptedFiles.Length];
            for (int i = 0; i < encryptedFiles.Length; i++)
            {
                decryptedFiles[i] = Encryption.Decrypt(encryptedFiles[i]);
            }
            return decryptedFiles;
        }

        /// <summary>
        /// Get a file chunk
        /// </summary>
        /// <returns>The file chunk</returns>
        public static byte[] GetFileChunk()
        {
            return server.GetFileChunk();
        }

        /// <summary>
        /// Get HDDs
        /// </summary>
        /// <returns>HDDs</returns>
        public static string[] GetHDDs()
        {
            KATS_ServerReference.ServerClient server = ServerConnection.MakeConnection();
            string[] encrypted = server.GetHDDs();
            server.Disconnect();
            string[] decrypted = new string[encrypted.Length];
            for (int i = 0; i < encrypted.Length; i++)
            {
                decrypted[i] = Encryption.Decrypt(encrypted[i]);
            }
            return decrypted;
        }

        /// <summary>
        /// Get the sub directories
        /// </summary>
        /// <returns>The subdirectories</returns>
        public static string[] GetSubDirectories()
        {
            KATS_ServerReference.ServerClient server = ServerConnection.MakeConnection();
            string[] encrypted = server.GetSubDirectories();
            server.Disconnect();
            string[] decrypted = new string[encrypted.Length];
            for (int i = 0; i < encrypted.Length; i++)
            {
                decrypted[i] = Encryption.Decrypt(encrypted[i]);
            }
            return decrypted;
        }

        /// <summary>
        /// Get the subdirectories
        /// </summary>
        /// <param name="Directory">The directory</param>
        /// <returns>The subdirectories</returns>
        public static string[] GetSubDirectoriesS(string Directory)
        {
            KATS_ServerReference.ServerClient server = ServerConnection.MakeConnection();
            string[] encrypted = server.GetSubDirectoriesS(Encryption.Encrypt(Directory));
            server.Disconnect();
            string[] decrypted = new string[encrypted.Length];
            for (int i = 0; i < encrypted.Length; i++)
            {
                decrypted[i] = Encryption.Decrypt(encrypted[i]);
            }
            return decrypted;
        }

        /// <summary>
        /// Move an object
        /// </summary>
        /// <param name="target">The target position</param>
        /// <returns>Successful?</returns>
        public static bool Move(string target)
        {
            string msg = Encryption.Encrypt(target);
            bool responce = server.Move(msg);
            return responce;
        }

        /// <summary>
        /// Open a file reader
        /// </summary>
        public static void OpenReader()
        {
            server.OpenReader();
        }

        /// <summary>
        /// Open a file writer
        /// </summary>
        public static void OpenWriter()
        {
            server.OpenWriter();
        }

        /// <summary>
        /// Print a message out on the server
        /// </summary>
        /// <param name="msg">The message to print</param>
        public static void PrintMessage(string msg)
        {
            KATS_ServerReference.ServerClient server = ServerConnection.MakeConnection();
            server.PrintMessage(Encryption.Encrypt(msg));
            server.Disconnect();
        }

        /// <summary>
        /// Rename a file on the server using a new server connection
        /// </summary>
        /// <param name="target">Target file</param>
        /// <param name="newTarget">New target file</param>
        /// <returns>Successful?</returns>
        public static bool Rename(string target, string newTarget)
        {
            KATS_ServerReference.ServerClient server = ServerConnection.MakeConnection();
            bool r = server.Rename(Encryption.Encrypt(target), Encryption.Encrypt(newTarget));
            server.Disconnect();
            return r;
        }

        /// <summary>
        /// Reset the file writer to the start of the file
        /// </summary>
        public static void ResetWriter()
        {
            server.ResetWriter();
        }

        /// <summary>
        /// Send a file chunk to the server
        /// </summary>
        /// <param name="fileBytes">The file chunk</param>
        /// <returns>Successful?</returns>
        public static bool SendFileChunk(byte[] fileBytes)
        {
            return server.SendFileChunk(fileBytes);
        }

        /// <summary>
        /// Set the current pointer location
        /// </summary>
        /// <param name="location">The location</param>
        /// <returns>Successful?</returns>
        public static bool SetLocation(string location)
        {
            return server.SetLocation(Encryption.Encrypt(location));
        }

        /// <summary>
        /// Undo a read operation
        /// </summary>
        /// <returns>Successful?</returns>
        public static bool UndoRead()
        {
            return server.UndoRead();
        }

        /// <summary>
        /// Disconnect from the server
        /// </summary>
        public static void Disconnect()
        {
            server.Disconnect();
        }
    }
}
